﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using System.Net;
using System.Diagnostics;
using ICSharpCode.SharpZipLib;

namespace NewsWorm {
    internal class UpdateManager {

        internal static bool CheckForUpdate( string CurrentVersion ) {

            try {
                string url = "http://www.newsworm.net/update/?version=" + CurrentVersion;
                string ResponseText;
                bool retval = false;

                HttpWebRequest httpWebRequest =
                   (HttpWebRequest)WebRequest.Create( url );
                httpWebRequest.ContentType = "text/html";
                httpWebRequest.Method = "GET";

                HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using ( var streamReader = new StreamReader( httpResponse.GetResponseStream() ) ) {
                    ResponseText = streamReader.ReadToEnd();
                }

                switch ( ResponseText ) {
                    case "Update available":
                        retval = true;
                        break;
                }

                return retval;

            } catch ( Exception e ) {
                new MyMessageBoxError( e.Message, e.StackTrace ).ShowDialog();
                return false;
            }
        }

        private static string GetTemporaryDirectory() {
            string tempDirectory = Path.Combine( Path.GetTempPath(), Path.GetRandomFileName() );
            Directory.CreateDirectory( tempDirectory );
            return tempDirectory;
        }

        //Only download stable versions, beta versions will not raise an update notification(not marked in the database)
        internal static void DownloadAndInstallUpdate(IMainWindowClient window) {
            //Unzip the file and execute using process.start
            try {
                string url = "http://www.newsworm.net/SetupNewsWorm.zip";
                string temp = Path.GetTempFileName() + ".zip";
                string extTemp = GetTemporaryDirectory() + "\\";
                WebClient client = new WebClient();
                client.DownloadFile( url, temp );
                ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();
                fz.ExtractZip( temp, extTemp, null);
                Process.Start( extTemp + "SetupNewsWorm.exe" ); //needs to be an .exe
                window.AppClose();
            } catch ( Exception ex ) {
                new MyMessageBoxError( ex.Message, ex.StackTrace ).ShowDialog();
            }
        }

    }
}
